/*
 * @desc:
 * @author: mao
 * @version: 1.0
 * @Date: 2023-06-29 09:42:46
 */
import { AxiosInstance, AxiosStatic } from "axios";
// import { getMediainfo } from "../../utils/mediainfo";
import COS from "cos-js-sdk-v5";

let request: AxiosStatic | AxiosInstance;
export const uploadQiniu = async (
    file: File,
    dirName?: string,
    hasToken?: string,
    successCallback?: any,
    progressCallback?: any,
    isRandomFileName = false,
    mime = "text/plain",
) => {
    console.log(file, 11111);
    let tencentToken: any;
    if (!hasToken) {
        tencentToken = await request({
            url: "/mediaresourcelib-file-manager/auth/cos/tokens",
            method: "get",
        });
    } else {
        tencentToken = hasToken;
    }
    let key = isRandomFileName ? Date.now() + file.name : file.name;
    return new Promise(async (resolve, reject) => {
        const tencentCos = new COS({
            getAuthorization: function (options, callback) {
                callback({
                    TmpSecretId: tencentToken.data.result.data.tmpSecret_id,
                    TmpSecretKey: tencentToken.data.result.data.tmp_secret_key,
                    SecurityToken: tencentToken.data.result.data.security_token,
                    StartTime: tencentToken.data.result.data.start_time,
                    ExpiredTime: tencentToken.data.result.data.expired_time,
                });
            },
        });

        tencentCos.uploadFile(
            {
                Bucket: "smartml-cd-1252627319",
                Region: "ap-chengdu",
                Key: !dirName ? key : `${dirName}${key}`,
                Body: file,
                onTaskReady: function (taskId) {
                    console.log(taskId);
                },
                onProgress: function (progressData) {
                    if (progressCallback)
                        progressCallback(progressData.percent);
                },
                onFileFinish: function (err, data, options) {
                    console.log(options.Key + "上传" + (err ? "失败" : "完成"));
                },
            },
            async function (err, data: any) {
                console.log(err || data);
                if (data && data.statusCode == 200) {
                    if (successCallback) successCallback();
                    // const fileInfo = await getMediainfo(file);
                    const fileInfo = {};
                    resolve({
                        ...data,
                        key: data.Location.replace(
                            "smartml-cd-1252627319.cos.ap-chengdu.myqcloud.com/",
                            "",
                        ),
                        mediainfo: fileInfo,
                    });
                }
            },
        );
    });
};

// 获取临时预览地址
export const tempUrl = async (objectKey: string) => {
    const res = await request({
        url: `/mediaresourcelib-file-manager/auth/qn/downUrls?objectKey=${objectKey}`,
        method: "get",
    });

    return res.data.result.data;
};

export function initQiniuOssAxios(axios: AxiosStatic | AxiosInstance) {
    console.log(axios);
    request = axios;
}
